{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "def process(fname,data_names):\n",
    "    dnames_remain=data_names.copy()\n",
    "    res_dict=dict()\n",
    "    with open(fname,'r') as fp:\n",
    "        for line in fp:\n",
    "            if len(line)>7:\n",
    "                s_l=line[21:].strip().split(',')\n",
    "                ind=0\n",
    "                while ind<len(s_l)-1:\n",
    "                    if '(' in s_l[ind] and ')'in s_l[ind+1]:\n",
    "                        s_l[ind]=s_l[ind]+','+s_l[ind+1]\n",
    "                        s_l.pop(ind+1)\n",
    "                    ind+=1\n",
    "                del ind\n",
    "                data_name=s_l[0].split(' ')[0].strip()\n",
    "                if data_name in dnames_remain:\n",
    "                    # print(s_l[0]+s_l[1]+s_l[3])\n",
    "                    dnames_remain.remove(data_name)\n",
    "                    res_dict[data_name]=pd.DataFrame([],columns=['k_neighbors', 'nmi', 'ari', 'ami', 'time_cost'])\n",
    "                end_ind=res_dict[data_name].shape[0]\n",
    "                res_dict[data_name].loc[end_ind,'k_neighbors']=s_l[2].strip().split('=')[-1]\n",
    "                res_dict[data_name].loc[end_ind,'nmi']=float(s_l[4].strip().split('=')[-1])\n",
    "                res_dict[data_name].loc[end_ind,'ari']=float(s_l[5].strip().split('=')[-1])\n",
    "                res_dict[data_name].loc[end_ind,'ami']=float(s_l[6].strip().split('=')[-1])\n",
    "                res_dict[data_name].loc[end_ind,'time_cost']=float(s_l[7].strip().split('=')[-1][:-1])\n",
    "    return res_dict,dnames_remain\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  alg_name data_name k_neighbors nmi ari ami time_cost\n",
      "0      NaN       NaN         NaN   1   2   3       NaN\n"
     ]
    }
   ],
   "source": [
    "wass_kinds=[\"in\",\"out\",\"out_out\"]#[\"out_out\"]\n",
    "alg_names=[\"WassDPC\",\"FastDEC\",\"QuickDSC\",\"QuickshiftPP\",\"SNNDPC\",\"FINCH\",\"QuickShift\",\"DBSCAN\",\"MeanShift\"]\n",
    "data_names=[\"R15\",\"S2\",\"Flame\",\"banana-ball\",\"seeds\",\"banknote\",\"segmentation\",\"phoneme\",\"MFCCs\",\"mnist_784\"]\n",
    "log_path_base=\"./data/others/\"\n",
    "exp_res=pd.DataFrame([],columns=['alg_name','data_name','k_neighbors', 'nmi', 'ari', 'ami', 'time_cost'])\n",
    "\n",
    "for alg_n in alg_names:\n",
    "    log_file=log_path_base+alg_n+\"_log.txt\"\n",
    "    res_dict,dnames_remain=process(log_file,data_names)\n",
    "    if len(dnames_remain)>0:\n",
    "        print(alg_n,\"has no result of\",dnames_remain,'!!!')\n",
    "    for data_name in res_dict.keys():\n",
    "        for key_col in ['nmi', 'ari', 'ami']:\n",
    "            i = res_dict[data_name][key_col].astype(float).argmax()\n",
    "            end_ind=exp_res.shape[0]\n",
    "            exp_res.loc[end_ind,'alg_name']=alg_n\n",
    "            exp_res.loc[end_ind,'data_name']=data_name\n",
    "            exp_res.loc[end_ind,'k_neighbors']=res_dict[data_name].loc[i,'k_neighbors']\n",
    "            exp_res.loc[end_ind,'nmi']=res_dict[data_name].loc[i,'nmi']\n",
    "            exp_res.loc[end_ind,'ari']=res_dict[data_name].loc[i,'ari']\n",
    "            exp_res.loc[end_ind,'ami']=res_dict[data_name].loc[i,'ami']\n",
    "            exp_res.loc[end_ind,'time_cost']=res_dict[data_name].loc[i,'time_cost']\n",
    "exp_res.to_csv(log_path_base+\"exp_res.csv\",sep='\\t')\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.11 ('Hongzhang_env')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "dcbeeb8ca79c01323ae54b865a63ae10d4e6f7bd4147a47205476fda702fa7ae"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
